(function(a) {
	a.fn.spinner = function(b) {
		var element = this;
		return this.each(function() {
			var f = {
				value: 0,
				min: 0
			};
			//console.log('f:', f);
			//console.log('b:', b);
			if(typeof b.value == "undefined"){
				b.value = element.val()
			}
			var k = a.extend(f, b);
			var j = {
				up: 38,
				down: 40
			};
			var d = a("<div></div>");
			d.addClass("input-group bootstrap-touchspin bootstrap-touchspin-injected");
			var l = a(this).addClass("value").attr("maxlength", "2").val(k.value).bind("keyup paste change",
			function(o) {
				var p = a(this);
				if (o.keyCode == j.up) {
					c(1)
				} else {
					if (o.keyCode == j.down) {
						c( - 1)
					} else {
						if (g(p) != d.data("lastValidValue")) {
							n(p)
						}
					}
				}
			});
			l.wrap(d);
			var h = a('<span class="input-group-btn input-group-append"><button class="btn btn-success btn-xs increase" type="button"><i class="iconfontzz icon-plus"></i></button></span>').click(function() {
				c(1)
			});
			var e = a('<span class="input-group-btn input-group-prepend"><button class="btn btn-danger btn-xs decrease" type="button"><i class="iconfontzz icon-minus"></i></button></span>').click(function() {
				c( - 1)
			});
			m(l);
			d.data("lastValidValue", k.value);
			l.before(e);
			l.after(h);
			function c(o) {
				l.val(g() + o);
				n(l)
			}
			function n(o) {
				clearTimeout(d.data("timeout"));
				var p = m(o);
				if (!i(p)) {
					l.trigger("update", [o, p])
				}
			}
			function m(o) {
				var q = g();
				if (q <= k.min) {
					e.attr("disabled", "disabled")
				} else {
					e.removeAttr("disabled")
				}
				o.toggleClass("invalid", i(q)).toggleClass("passive", q === 0);
				if (i(q)) {
					var p = setTimeout(function() {
						l.val(d.data("lastValidValue"));
						m(o)
					},
					500);
					d.data("timeout", p)
				} else {
					d.data("lastValidValue", q)
				}
				return q
			}
			function i(o) {
				return isNaN( + o) || o < k.min
			}
			function g(o) {
				o = o || l;
				return parseInt(o.val() || 0, 10)
			}
		})
	}
})(jQuery);